package chp12;

import java.util.Arrays;

public class Tool5 {

    public static void main(String[] args) {
        int[] nums = {34,63,2,56,22};
        System.out.println(Arrays.toString(nums));
        //保留精度的平均值 35.4
        AvgAndAboveItems avgAndAboveItems = getAvgAndAbove(nums);
        System.out.println("平均值:" + avgAndAboveItems.avg);
        System.out.println("超过平均值的内容:");
        System.out.println(Arrays.toString(avgAndAboveItems.nums));
    }

    //需要找到数组的平均值(保留精度)和数组里面超过平均值的元素的数组
    public static AvgAndAboveItems getAvgAndAbove(int[] nums){
        double sum =0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        double avg = sum/nums.length;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] > avg){
                count++;
            }
        }
        int[] aboveNums = new int[count];
        for (int i = 0,j=0; i < nums.length; i++) {
            if(nums[i] > avg){
                aboveNums[j] = nums[i];
                j++;
            }
        }
        AvgAndAboveItems avgAndAboveItems = new AvgAndAboveItems();
        avgAndAboveItems.avg = avg;
        avgAndAboveItems.nums = aboveNums;
        return avgAndAboveItems;
    }
}
